package day05;

public class 合并两个有序链表 {
    class Solution {
        public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
            if (list1 == null && list2 == null) {
                return null;
            }
            if (list1 == null && list2 != null) {
                return list2;
            }
            if (list1 != null && list2 == null) {
                return list1;
            }
            // 创建一个虚拟头结点
            ListNode head = new ListNode(0);
            ListNode cur = head;
            while (list1 != null && list2 != null) {
                // 进行比较
                if (list1.val <= list2.val) {
                    cur.next = list1;
                    cur = list1;
                    list1 = list1.next;
                } else {
                    cur.next = list2;
                    cur = list2;
                    list2 = list2.next;
                }

            }
            if (list1 != null) {
                cur.next = list1;
            }
            if (list2 != null) {
                cur.next = list2;
            }
            return head.next;
        }
    }
}
